﻿using Furion.DatabaseAccessor;
using Furion.DatabaseAccessor.Extensions;
using Furion.DependencyInjection;
using Furion.FriendlyException;
using HOPU.OnlineSchool.Application.Teachers.Dto;
using HOPU.OnlineSchool.Core;
using Mapster;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Yitter.IdGenerator;

namespace HOPU.OnlineSchool.Application.Teachers.Services
{
    public class TeacherServices : ITeacherServices, ITransient
    {
        private readonly IRepository<EduTeacher> _teacherEepository;

        public TeacherServices(IRepository<EduTeacher> teacherEepository)
        {
            _teacherEepository = teacherEepository;
        }

        /// <summary>
        /// 添加讲师
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task AddAsync(TeacherAddInput input)
        {
            var model = input.Adapt<EduTeacher>();

            //通过雪花算法生成主键id
            model.Id = YitIdHelper.NextId().ToString();
            await _teacherEepository.InsertAsync(model);
        }

        public async Task DeleteAsync(string id)
        {
            var model = await _teacherEepository.FirstOrDefaultAsync(e => e.Id == id && e.IsDeleted == 0);
            if(model==null)
                throw Oops.Oh("该讲师已被删除");
            model.IsDeleted = 1;
            model.GmtModified = DateTime.Now;
            //更新指定的字段信息
            await _teacherEepository.UpdateIncludeAsync(model, new string[] { nameof(model.IsDeleted), nameof(model.GmtModified) });
        }

        public async Task<PagedList<TeacherListOutput>> PagedListAsync(int page, int limit, TeacherListInput input)
        {
            var quary = _teacherEepository.Where(e => e.IsDeleted == 0);
            if (!string.IsNullOrWhiteSpace(input.Name))
                quary = quary.Where(e => e.Name.Contains(input.Name));
            if (input.Level>0)
                quary = quary.Where(e => e.Level==input.Level);
            if (input.Begin.HasValue)
                quary = quary.Where(e => e.GmtCreate>=input.Begin.Value);
            if (input.End.HasValue)
                quary = quary.Where(e => e.GmtModified<=input.End.Value);
            var list =await quary.OrderByDescending(e => e.GmtCreate).ToPagedListAsync(page, limit);
            return list.Adapt<PagedList<TeacherListOutput>>();
        }

        public async Task<TeacherAddInput> GetTeacherById(string id)
        {
            var model =await _teacherEepository.FirstOrDefaultAsync(e => e.IsDeleted == 0 && e.Id == id);
            if (model == null)
                throw Oops.Oh("该教师不存在或已注销");
            return model.Adapt<TeacherAddInput>();

        }

        public async Task UpdateTeacherById(string id,TeacherAddInput dto)
        {
            var exists = await _teacherEepository.AnyAsync(t => t.Id == id && t.IsDeleted == 0);
            if (!exists)
                throw Oops.Oh("该讲师未找到");
            var teacher = dto.Adapt<EduTeacher>();
            await teacher.UpdateExcludeAsync(new[] { nameof(teacher.GmtCreate), nameof(teacher.IsDeleted) });
        }
    }
}
